class Login {
    constructor() {
        this.systemLocalStorage = new SystemLocalStorage();
        this.changeLocale();
        this.alert = document.querySelector('sl-alert');
        this.baseConfig = new BaseConfig();
        this.encrypt = new Encrypt({ baseConfig: this.baseConfig });
        this.ajax = new Ajax();
        this.toast = new Toast();
    }
    async changeLocale() {
        let that = this;
        const localeSelect = document.getElementById('select-locale');
        localeSelect.value = that.systemLocalStorage.locale;
        localeSelect.addEventListener('sl-change', event => {
            that.systemLocalStorage.locale = localeSelect.value;
            location.reload();
        });
    }
    doLogin({ username, password }) {
        let that = this;
        password = this.encrypt.encrypt(password);
        that.ajax.post({
            type: "POST",
            url: "/accountpermissionsmanagement/login",
            data: {
                key: this.baseConfig.privateKey,
                username: username,
                password: password
            },
            beforeCallback: function(){
                that.loading.exhibit = !that.loading.exhibit;
            },
            successCallback: function (res) {
                if (res.body && res.body.length == 1) {
                    location = "/pages/home/index.html";
                } else {
                    that.toast.notify(res.message, "danger", 6000);
                }
            },
            errorCallback: function (error) {
                console.log(error);
            },
            completeCallback: function () {
                that.loading.exhibit = !that.loading.exhibit;
            }
        })
    }
}
document.addEventListener('DOMContentLoaded',
    async function () {
        const localeUtil = new Locale();
        const systemLocalStorage = new SystemLocalStorage();
        await localeUtil.translate(systemLocalStorage.locale, "template-login", "content-login");
        const login = new Login();
        login.loading = document.querySelector('wcs-loading-bounce');
        const loginButton = document.getElementById('button-login');
        const usernameInput = document.getElementsByName('username')[0];
        const passwordInput = document.getElementsByName('password')[0];
        loginButton.addEventListener("click", event => {
            let username = usernameInput.value;
            let password = passwordInput.value;
            login.doLogin({
                username: username,
                password: password
            });
        });
        usernameInput.addEventListener('keydown', function (event) {
            if (event.keyCode === 13) {
                if(this.value){
                    usernameInput.blur();
                    passwordInput.focus();
                }
            }
        });
        passwordInput.addEventListener('keydown', function (event) {
            if (event.keyCode === 13) {
                if(this.value){
                    let username = usernameInput.value;
                    let password = passwordInput.value;
                    login.doLogin({
                        username: username,
                        password: password
                    });
                }
            }
        });
    }
);

